<?php
/**
* @version 0.1
* @package dok
* @copyright Derek OKeeffe.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* 
* Used to integrate Joomla with Knowledgetree. 
* 
*/
define( '_VALID_MOS', 1 );

include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );

$mainframe = new mosMainFrame( $database, '', '.' );

$action = $_GET['action'];

//Only allow access from localhost. 
//Nothing else should access this function except for the knowledgetree interceptor (JoomlaPlugin.php).
$requestor = getenv('REMOTE_ADDR');
if($requestor != '127.0.0.1'){
      die( 'Restricted access' );
}

switch ($action) {
	case 'getCookieName':
		echo md5( 'site' .  $mainframe->getCfg( 'live_site' ) );
		break;
	case 'getUserName':
	    $session = new mosSession($database);
	    $sessid = $_GET['sessid'];
	    $ip = $_GET['ip'];
	    $agent = $_GET['agent'];
	    $debug = $_GET['debug'];
	    $encryptedCookieValue = mosHash($sessid . $ip . $agent);
		if($debug=='true'){
			echo 'Using the following to get the encrypted session from the DB:<br> sessid='.$sessid. '<br> ip='.$ip . '<br> agent='.$agent;
			echo '<br>Hashed the 3 values above and now going to get session '. $encryptedCookieValue .' from the database';
		}
		$session->load($encryptedCookieValue);
		if($debug=='true') {
			echo '<p>Found user: ';
		}
		echo $session->username;
		break;
	default:
		echo 'Invalid Action';
}
?>
